﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Models;

namespace SuperQuery.UserControls.AddDatabaseWizard
{
    /// <summary>
    /// Interaction logic for SelectDatabasesView.xaml
    /// </summary>
    public partial class SelectDatabasesView : UserControl, WizardControl.Views.IPage, Views.ISelectDatabasesView
    {
        /// <summary>
        /// Initilise the selected databases user control with a list of existing databases that have been selected
        /// </summary>
        /// <param name="existingDatabases">The list of databases that are currently selected and which should not be offered
        /// to the user for selection</param>
        public SelectDatabasesView(List<Database> existingDatabases)
            : this()
        {
            // Hook up the DataContext
            DataContext = this;

            m_Presenter = new Presenters.SelectDatabases(this, existingDatabases);
        }

        /// <summary>
        /// Default constructor for the selected databases user control
        /// </summary>
        private SelectDatabasesView()
        {
            InitializeComponent();
        }

        #region Implementation of IPage

        /// <summary>
        /// An object containing data that this page can use
        /// </summary>
        public object InputData
        {
            get;
            set;
        }

        /// <summary>
        /// An object containing data that will be passed to the InputData property of the next page
        /// </summary>
        public object OutputData
        {
            get
            {
                return m_Presenter.GetSelectedDatabases();
            }
            set
            {
                // This is a read only property for this page so do nothing
                // This "set" section has to be here becuase of the IPage interface
            }
        }

        /// <summary>
        /// The number of this page
        /// </summary>
        /// <returns>A page number</returns>
        public int PageNumber()
        {
            return WizardControl.Wizard.FINISH_PAGE;
        }

        /// <summary>
        /// The caption to use for this page
        /// </summary>
        /// <returns>A page caption</returns>
        public string PageCaption()
        {
            return "Select database(s) to add";
        }

        /// <summary>
        /// The state of the Finish button
        /// </summary>
        /// <returns>If true then Finishg will be active</returns>
        public bool AllowFinish()
        {
            return true;
        }

        /// <summary>
        /// The state of the Cancel button
        /// </summary>
        /// <returns>If true then the Cancel button will be active</returns>
        public bool AllowCancel()
        {
            return true;
        }

        /// <summary>
        /// The state of the Back button
        /// </summary>
        /// <returns>If true then the Back button will be active</returns>
        public bool AllowBack()
        {
            return true;
        }

        /// <summary>
        /// The state of the Forward button
        /// </summary>
        /// <returns>If true then the Forward button will be active</returns>
        public bool AllowForward()
        {
            return false;
        }

        /// <summary>
        /// Validate data on the current page
        /// </summary>
        /// <returns>If true then the next page will be displayed, otherwise we remain on the current page</returns>
        public bool ValidatePage()
        {
            // No validation needed for this page so always return true
            return true;
        }

        /// <summary>
        /// The next page to progress to when forward is clicked
        /// </summary>
        /// <returns>The next page number</returns>
        public int NextPageNumber()
        {
            // This is the last page so if for some reason we try to get the next page number
            // we will simply get our current page number
            return PageNumber();
        }

        /// <summary>
        /// Called just before the page is displayed
        /// </summary>
        public void PageActivated()
        {
            // The Input property should contain a server model which is the model we will use
            if ((InputData != null) && (InputData is Server))
            {
                m_Presenter.LoadAvailableDatabases((Server)InputData);
            }
        }

        /// <summary>
        /// Called just after the page is validated, before the next page is displayed
        /// </summary>
        public void PageDeactivated()
        {
            // Nothing to do here for this page
            return;
        }

        #endregion

        #region Implementation of ISelectDatabasesView

        /// <summary>
        /// List of databases available for selecting
        /// </summary>
        public List<Database> AvailableDatabases
        {
            get
            {
                return (List<Database>)GetValue(AvailableDatabasesProperty);
            }
            set
            {
                SetValue(AvailableDatabasesProperty, value);
            }
        }

        /// <summary>
        /// Display an error message to the user
        /// </summary>
        /// <param name="message">The error message to display</param>
        /// <param name="title">The title to use for the error message</param>
        public void DisplayError(string message, string title)
        {
            MessageBox.Show(message, title, MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for AvailableDatabases.  This enables animation, styling, binding, etc... 
        /// </summary>
        private static readonly DependencyProperty AvailableDatabasesProperty =
            DependencyProperty.Register("AvailableDatabases", typeof(List<Database>), typeof(SelectDatabasesView), new UIPropertyMetadata(null));


        #endregion

        /// <summary>
        /// Select None of the displayed databases
        /// </summary>
        /// <param name="sender">Who raised the event</param>
        /// <param name="e">Details about the event</param>
        private void btnSelectNone_Click(object sender, RoutedEventArgs e)
        {
            m_Presenter.SelectNone();
        }

        /// <summary>
        /// Select All of the displayed databases
        /// </summary>
        /// <param name="sender">Who raised the event</param>
        /// <param name="e">Details about the event</param>
        private void btnSelectAll_Click(object sender, RoutedEventArgs e)
        {
            m_Presenter.SelectAll();
        }

        /// <summary>
        /// Presenter for this class
        /// </summary>
        private readonly Presenters.SelectDatabases m_Presenter;
    }
}
